package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.exception.ServiceException;
import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageParam;
import com.example.lotterysystem.controller.result.FindPrizeListResult;
import com.example.lotterysystem.service.PictureService;
import com.example.lotterysystem.service.PrizeService;
import com.example.lotterysystem.service.dto.PageListDTO;
import com.example.lotterysystem.service.dto.PrizeDTO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@RestController

public class PiczeController {
    // 日志
    private final static Logger logger = LoggerFactory.getLogger(PiczeController.class);
    // 注入图片服务
    @Autowired
    private PictureService pictureService;
    @Autowired
    // 注入奖品服务
    private PrizeService prizeService;

    /**
     * 上传 图片
     * @param file
     * @return
     */
    @RequestMapping("/pic/upload")
    public String uploadPic(MultipartFile file){
        return pictureService.savePicture(file);
    }

    /**
     * 创建奖品
     *  RequestPart:用于接收表单数据的 multipart/form-data
     *
     * @param param
     * @param picFile
     * @return
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@Valid @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile picFile) {
        logger.info("createPrize CreatePrizeParam:{}",
                JacksonUtil.writeValueAsString(param));
        return CommonResult.success(
                prizeService.createPrize(param, picFile));
    }
    /**
     * 查询奖品列表
     * @param param
     * @return
     */
    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param){
        logger.info("findPrizeList FindPrizeListParam:{}",JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPrizeList(param);
        return CommonResult.success(converToFindPrizeListResult(pageListDTO));
    }

    /**
     *  将PageListDTO<PrizeDTO> 转换为 indPrizeListResult
     * @param pageListDTO
     * @return
     */
    private FindPrizeListResult converToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {

    if(null == pageListDTO){
        throw new ServiceException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
    }
    // 创建FindPrizeListResult对象，用于封装转换后的数据
    FindPrizeListResult result = new FindPrizeListResult();
    result.setTotal(pageListDTO.getTotal());// 设置总记录数
    // 将每个PrizeDTO对象转换为FindPrizeListResult.PrizeInfo对象，并收集到列表中
    result.setRecords(pageListDTO.getRecords().stream()
            .map(prizeDTO -> {
                // 创建FindPrizeListResult.PrizeInfo对象，用于封装单个奖项的信息
                FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                prizeInfo.setPrizeName(prizeDTO.getName());
                prizeInfo.setDescription(prizeDTO.getDescription());
                prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                prizeInfo.setPrice(prizeDTO.getPrice());
                return prizeInfo;
            }).collect(Collectors.toList()));
    // 返回封装好的FindPrizeListResult对象
    return result;
}

}
